/**
 * 管理员角色
 * @author yangjian
 */
define(function(require, exports) {

	//加载依赖
	require("ajaxproxy");
	require("layer");

	var common = require("common");

	//初始化AjaxProxy插件
	$('.ajaxproxy').AjaxProxy({
		dataType : "json",
		method : "post",
		formId : "content-add-form",
		formCheckHandler : function(form_id) {
			return common.formCheckHandler(form_id, true);
		},
		callbackDelay : __global.jdialog.timer,
		timeInterval : __global.jdialog.timer,
		callBack : function (data) {   /* 执行ajax之后的回调函数 */
			common.ajaxCallback(data);
		}
	});

	exports.init = function(){


		require('editor');
		var editor;
		editor = KindEditor.create('#editor-content', {
			uploadJson : 'kindeditor_upload',
			allowFileManager : true,
			afterBlur: function(){this.sync();}
		});

		loadupload('files-upload','drop-area','src',function(data){$('input[name="data[surface_image]"]').val(data.message);});

		function loadupload(filesUploadId,dropAreaId,formInputName,callbak) {

			var filesUpload = $('#'+filesUploadId);
			if (filesUpload) {

				var fileInput = filesUpload.css({height: 0, width: 0, 'overflow': 'hidden'});

				filesUpload.bind("change",function(){
					traverseFiles(this.files,filesUploadId,dropAreaId,formInputName,callbak);
				});

				$('#'+dropAreaId).click(function () {
					fileInput.click();
				}).show();
			}
		}


		function traverseFiles(files,filesUploadId,dropAreaId,formInputName,callbak) {
			if (typeof files !== "undefined") {

				file = files[0];
				uploadFile(files[0],filesUploadId,dropAreaId,formInputName,callbak);

			} else {
				fileList.innerHTML = "No support for the File API in this web browser";
			}
		}

		function uploadFile(file,filesUploadId,dropAreaId,formInputName,callback) {

			var img = document.createElement("img");
			var reader = new FileReader();

			if (typeof FileReader !== "undefined" && (/image/i).test(file.type)) {
				var size = document.getElementById(filesUploadId).files[0].size;


				reader.onload = (function (theImg) {
					var fd = new FormData();

					fd.append(formInputName,document.getElementById(filesUploadId).files[0]);

					exports.ajaxResultByBinary(
						'/admin/upload/upload?fileInputName='+formInputName,
						fd,
						function (data) {
							callback(data);
						});
					theImg.onload = function () {

						$("#"+dropAreaId).html(img);

					}
					return function (evt) {
						theImg.src = evt.target.result;
					};
				}(img));

				reader.readAsDataURL(file);
			}
		}

	};


	exports.ajaxResultByBinary = function (url, data, callback, failback) {

		$.ajax({
			type: 'POST',
			url: url,
			data: data,
			async: false,
			cache: false,
			contentType: false,
			processData: false,
			dataType: 'json',

			success: function (thisData) {

				layer.closeAll("loading");

				if (thisData.code == '1') {
					if ($.isFunction(failback)) {
						failback(thisData);
					} else {
						layer.alert(thisData.message, {
							icon: 2, timeout: 1800, end: function () {

							}
						});

					}

				} else {
					if ($.isFunction(callback)) {
						callback(thisData);
					} else {
						layer.msg(thisData.message, {
							icon: 1, timeout: 1800, end: function () {
								location.reload(true);
							}
						});
					}
				}
			},
			error: function (xhr, ajaxOptions, thrownError) {
				layer.closeAll("loading");
				layer.alert("系统出了小差，请联系管理员！", {icon: 2});
			}
		});
	};
});
